package  org.cocos2dx.javascript.Advert.core;
import android.app.Activity;
import android.util.Log;
import android.widget.Toast;

import org.cocos2dx.javascript.Advert.gdt.GDTSdkComponent;
import org.cocos2dx.javascript.NativeMgr;
import org.cocos2dx.lib.Cocos2dxActivity;
import org.cocos2dx.lib.Cocos2dxJavascriptJavaBridge;

import org.cocos2dx.javascript.AppActivity;
import org.json.JSONObject;

public class AdSdkBridge {

    static Activity curActivity;
    static AdSdkComponent adSdkComponent;
    private static AdSdkBridge mInstace = null;
    
     public void init () {
        //curActivity = AppActivity.getInstance();
    }


    public static AdSdkBridge getInstance() {

        
        if (null == mInstace) {
            AppActivity  curActivity = AppActivity.getInstance();
            GDTSdkComponent gdtSdkComponent = new GDTSdkComponent();
            gdtSdkComponent.init(curActivity);
            mInstace = new AdSdkBridge(curActivity,  gdtSdkComponent);
        } 
        mInstace.init();
        return mInstace;
    }



    public AdSdkBridge(Activity activity, AdSdkComponent adSdkComponent) {
        AdSdkBridge.curActivity = activity;
        AdSdkBridge.adSdkComponent = adSdkComponent;
        adSdkComponent.init(activity);
    }

    public static void onScript(String arg0) {
        Log.d("AdSdkBridge", "onScript: " + arg0 );
      
            switch (arg0) {
                case "loadSplashAd": {
                    AdBase ad = adSdkComponent.getSplashAd();
                    if (ad != null) {
                        ad.loadAd(curActivity, false);
                    }
                    break;
                }
                case "showSplashAd": {
                    AdBase ad = adSdkComponent.getSplashAd();
                    if (ad != null) {
                        ad.showAd(curActivity);
                    }
                    break;
                }
                case "loadRewardVideoAd": {
                    AdBase ad = adSdkComponent.getRewardVideoAd();
                    if (ad != null) {
                        ad.loadAd(curActivity, false);
                    }
                    break;
                }
                case "showRewardVideoAd": {
                    AdBase ad = adSdkComponent.getRewardVideoAd();
                    if (ad != null) {
                        ad.showAd(curActivity);
                    }
                    break;
                }
                case "loadInterstitialAd": {
                    AdBase ad = adSdkComponent.getInterstitialAd();
                    if (ad != null) {
                        ad.loadAd(curActivity, false);
                    }
                    break;
                }
                case "showInterstitialAd": {
                    AdBase ad = adSdkComponent.getInterstitialAd();
                    if (ad != null) {
                        ad.showAd(curActivity);
                    }
                    break;
                }
                case "loadBannerAd": {
                    AdBase ad = adSdkComponent.getBannerAd();
                    if (ad != null) {
                        ad.loadAd(curActivity, false);
                    }
                    break;
                }
                case "showBannerAd": {
                    AdBase ad = adSdkComponent.getBannerAd();
                    if (ad != null) {
                        ad.showAd(curActivity);
                    }
                    break;
                }
                case "closeBannerAd": {
                    AdBase ad = adSdkComponent.getBannerAd();
                    if (ad != null) {
                        ad.closeAd();
                    }
                    break;
                }
            }
        
    }



    public static void sendToScript(String arg0, String arg1) {
       // ((Cocos2dxActivity) activity).runOnGLThread(() -> Cocos2dxJavascriptJavaBridge.evalString("AdSdkComponent.onNative(\"" + arg0 + "\", \"" + arg1 + "\")"));

        try {

            JSONObject mJsonobjData = new JSONObject();
            mJsonobjData.put("arg0", arg0);
            mJsonobjData.put("arg1", arg1);

            NativeMgr.OnCallBackToJs("AdCB", mJsonobjData.toString());
        } catch (Exception e) {
                // TODO: handle exception
                Log.e("onResp", "onResp");
                Log.e("onResp Exception",e.toString());
                //e.printStackTrace();
        }
    }   

}
